{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}

<div class="demoTable">
    <br>
    <form class="layui-form" action="" id="workTable" lay-filter="workTable">

        <div class="layui-input-inline">
            <label class="layui-form-label">project_name：</label>
            <div class="layui-input-inline">
                <select class="layui-select" name="project_id" id="project_id" lay-verify="" lay-search
                                lay-filter="project_id">

                            {% for d in projectAll %}
                                <option value={{d.id}}>{{d.project_name}}</option>
                            {% endfor %}
                </select>
            </div>
                <button  class="layui-btn layui-btn-normal" id="save" lay-submit lay-filter="formDemo">保存</button>
        </div>
        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">module_name：</label>
            <div class="layui-input-inline" id="module_div">
                <select class="layui-select" name="module_id" id="module_id" lay-verify="" lay-search
                                lay-filter="module_id">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">plan_name：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="api_plan_name" id="api_plan_name" placeholder="请输入" style="width:212px">
            </div>
        </div>

        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">order：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="auto_order" id="auto_order" placeholder="cron:12或者interval:30" style="width:212px">
            </div>
        </div>
        <div class="layui-input-inline" id="l_case_id" style="display:none">

            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="id" id="id" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline" style="display:none" id="caseDiv">
            <label class="layui-form-label">caseIDList：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="caseIdList" id="caseIdList" placeholder="请输入" style="width:212px">
            </div>
        </div>

    </form>

</div>
<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;

          apiPlanCache = layui.data('test').apiPlanCache;
          if(apiPlanCache ==undefined){

          }else{
               form.val('workTable',{
                    id:apiPlanCache.id,
                    project_id:apiPlanCache.project_id,
                    module_id:apiPlanCache.module_id,
                    api_plan_name:apiPlanCache.api_plan_name,
                    auto_order:apiPlanCache.auto_order,
                    id:apiPlanCache.id,
                    caseIdList:apiPlanCache.caseIdList,
              });
              if(caseIdList!="" && caseIdList!=undefined){
                $("#caseDiv").show();
              }else{
                $("#caseDiv").hide();
              }
              $("#project_id").find("option[value='"+apiPlanCache.project_id+"']").attr("selected",true);

              $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/moudle/all/select?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",

                        data:{"project_id":apiPlanCache.project_id,"moudle_type":"1"},
                        success: function(result){
                            s1 = '<div class="layui-input-inline"> <select class="layui-select" name="module_id" id="module_id" lay-verify="" lay-search lay-filter="module_id"><option value="">请选择</option>';
                            e1 = '</select></div>';
                             if(result.data.length>0){

                                str = '';
                                layui.each(result.data,function(k,v){
                                        str = str+'<option value="'+v.id+'">'+v.moudle_name+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#module_div").html(htmlDiv);
                             }else{
                                $("#module_div").html(s1+e1);
                             }
                             $("#module_id").find("option[value='"+apiPlanCache.module_id+"']").attr("selected",true);
                             form.render();

                       }
              });
              form.render();
          }

          form.on('select(project_id)', function(obj){
                $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/moudle/all/select?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"project_id":obj.value,"moudle_type":"1"},
                        success: function(result){
                            s1 = '<div class="layui-input-inline"> <select class="layui-select" name="module_id" id="module_id" lay-verify="" lay-search lay-filter="module_id"><option value="">请选择</option>';
                            e1 = '</select></div>';
                             if(result.data.length>0){

                                str = '';
                                layui.each(result.data,function(k,v){
                                        str = str+'<option value="'+v.id+'">'+v.moudle_name+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#module_div").html(htmlDiv);


                             }else{
                                $("#module_div").html(s1+e1);
                             }
                             form.render();


                       }
                    });
                  form.render('select');
          });

          //form.on('select(param_type)', function(data){});

          //监听提交
          form.on('submit(formDemo)', function(data){
            if(data.field.api_plan_name == ""){
                    layer.msg("请输入一个任务名称");
                    return false;
            }
            if(data.field.auto_order == ""){
                layer.msg("请配置一个时间!");
                return false;
            }
            $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/case/api/plan/manage",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify(data.field),
                        success: function(result){
                            if(result.id){
                                $('#save').show();
                                layer.msg("保存成功，但不会立即生效，生效时间为每天9:00");
                            }else{
                                layer.msg("保存失败!");
                            }
                        }
            });

            return false;
          });

          form.render();

    });

</script>

{% endblock %}